package com.example.springdemo.thread;

import javax.xml.crypto.Data;
import java.util.Date;

public class ThreadWaitAndNotifyPractice {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("主线程");
        ThreadA threadA = new ThreadA();
        ThreadC threadC = new ThreadC(threadA);
//        Thread thread1 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(10000);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//                System.out.println("A运行"+new Date());
//            }
//        });
//        thread1.run();
//        ThreadC threadC  = new ThreadC();
        threadA.start();
        threadC.start();
        threadA.join();
//        long time = System.currentTimeMillis();
//        // A先运行
//        threadA.run();
//        try{
//            thread1.join();
//        }catch (Exception e){
//
//        }

        System.out.println("再次回到主线程");
//        ThreadB threadB  = new ThreadB(time,threadA);
//        threadB.run();
    }


}


class ThreadA extends Thread{
    @Override
    public void run() {
        try {
            System.out.println("A运行"+new Date());
            Thread.sleep(3000);
            System.out.println("A唤醒"+new Date());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}

class ThreadC extends Thread{
    ThreadA threadA;
    public ThreadC( ThreadA threadB) {
        this.threadA = threadB;

    }

    @Override
    public void run() {
        try {
            System.out.println("C运行"+new Date());
            threadA.join();
            System.out.println("C唤醒"+new Date());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}


class ThreadB implements Runnable{
    long time;
    ThreadA threadA;
    public ThreadB(long time, ThreadA threadA) {
        this.time = time;
        this.threadA = threadA;
    }

    @Override
    public void run() {
        try {
            System.out.println("得到B"+new Date());
            while (System.currentTimeMillis() - time >= 3000){
                threadA.notify();
            }
            System.out.println("B唤醒"+new Date());
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
